package com.world.cat.controller.api.doc;

import com.world.cat.model.generate.Auto;
import com.world.cat.model.sys.ApiDetail;
import com.world.cat.service.sys.ApiDetailService;
import com.world.common.pojo.Result;
import com.world.common.pojo.api.ApiInterface;
import com.world.common.pojo.tree.ZTreeNode;
import com.world.common.util.SessionUtil;
import com.world.common.util.SysUtil;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * Created by gang on 2016/12/24.
 */
@Controller
@RequestMapping(value = "/api/doc", name = "Api文档管理")

public class ApiDocController {
    private static final Logger logger = Logger.getLogger(ApiDocController.class);
    //server地址
    private static final String server = "localhost:8080";

    @Resource
    private ApiDetailService apiDetailService;

    @RequestMapping(value = "/home", name = "")
    public String home(Model model, HttpServletRequest request) throws Exception {
        model.addAttribute("server", server);
        model.addAttribute("edit", request.getParameter("edit"));
        return "api/doc/home";
    }

    @RequestMapping(value = "/standard", name = "")
    public String standard() throws Exception {
        return "api/doc/standard";
    }

    @RequestMapping(value = "/tree", name = "-tree")
    @ResponseBody
    public Result tree() throws Exception {
        ZTreeNode zTreeNode = apiDetailService.getDocTree(SessionUtil.getApiUserName(), null);
        return new Result(true, "成功", zTreeNode.getChildren());
    }

    @RequestMapping(value = "/detail", name = "-detail")
    @ResponseBody
    public Result detail(String url) throws Exception {
        if (SysUtil.isEmpty(url)) {
            return new Result(false, "url不能为空");
        }
        ApiInterface apiInterface = apiDetailService.getInterfaceDetail(url);
        return new Result(true, "成功", apiInterface);
    }

    @RequestMapping(value = "/hello", name = "你好")
    @ResponseBody
    public Result hello(Auto auto) throws Exception {
        System.out.println(SessionUtil.getApiUserName());
        System.out.println(SessionUtil.getUserName());
        String data = "你好";
        return new Result(true, "成功", data);
    }
}
